package com.aier.user.entity.userapp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import net.eicp.ghostboy.futil.StringUtil;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;

import com.aier.common.common.SystemDateFormatUtil;

public class BillRowMapper implements RowMapper<Bill>{
    private int type;
    public static final int AND_DATESTR = 1;

    public BillRowMapper() {
    }

    public BillRowMapper(int type) {
        this.type = type;
    }

    @Override
    public Bill mapRow(ResultSet rs, int rowNum) throws SQLException {
        Bill um = null;
        switch (type) {
            case AND_DATESTR:
                um = parseBill(new Bill(), rs);
                um.setCdate_str(SystemDateFormatUtil.formatDate(um.getCdate(), SystemDateFormatUtil.FORMAT_DATETIME));
                um.setCommodity_name(rs.getString("commodity_name"));
                break;
            default:
                um = parseBill(new Bill(), rs);
                um.setCdate_str(SystemDateFormatUtil.formatDate(um.getCdate(), SystemDateFormatUtil.FORMAT_DATETIME));
        }
        return um;
    }

    public static Bill parseBill(Bill s, ResultSet rs) throws SQLException {
        return parseBill(s, rs, null);
    }

    public static Bill parseBill(Bill s, ResultSet rs, String su)
        throws SQLException {

        if (StringUtil.isStringNull(su)) {
            su = "";
        } else {
            su += ".";
        }
        try {
            s.setBid(rs.getLong(su+"bid"));
        } catch (SQLException e) {
            s.setBid(null);
        }
        try {
            s.setCid(rs.getLong(su+"cid"));
        } catch (SQLException e) {
            s.setCid(null);
        }
        try {
            s.setNum(rs.getInt(su+"num"));
        } catch (SQLException e) {
            s.setNum(null);
        }
        try {
            s.setUid(rs.getLong(su+"uid"));
        } catch (SQLException e) {
            s.setUid(null);
        }
        try {
            s.setMoney(rs.getInt(su+"money"));
        } catch (SQLException e) {
            s.setMoney(null);
        }
        try {
            s.setCdate(rs.getTimestamp(su+"cdate"));
        } catch (SQLException e) {
            s.setCdate(null);
        }
        try {
            s.setState(rs.getShort(su+"state"));
        } catch (SQLException e) {
            s.setState(null);
        }
        try {
            s.setDescription(rs.getString(su+"description"));
        } catch (SQLException e) {
            s.setDescription(null);
        }
        return s;
    }

    public static Map<String, Object> getPar(Map<String, Object> par, Bill s) {
        par.put("bid", s.getBid());
        par.put("cid", s.getCid());
        par.put("num", s.getNum());
        par.put("uid", s.getUid());
        par.put("money", s.getMoney());
        par.put("cdate",s.getCdate());
        par.put("state", s.getState());
        par.put("description", s.getDescription());
        return par;
    }

    public static Map<String, Object> getArgs(Map<String, Object> args, Bill s) {
        args.put("bid",s.getBid());
        return args;
    }

    /**
     * 
     * 
     * @param tfp
     * @return
     */
    public static PreparedStatementCreator getPreparedStatementCreator(
            final Bill s) {
        return new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection con)
                throws SQLException {
                PreparedStatement ps = con.prepareStatement(parseSQL(s),
                        new String[] { "bid" });
                parsePS(ps, s);
                return ps;
            }

        };
    }

    private static String parseSQL(Bill s) throws SQLException {
        StringBuffer sql = new StringBuffer("insert into userapp_bill(");
        int count = 0;
        if (null != s.getBid()) {
            sql.append("bid,");
            count++;
        }
        if (null != s.getCid()) {
            sql.append("cid,");
            count++;
        }
        if (null != s.getNum()) {
            sql.append("num,");
            count++;
        }
        if (null != s.getUid()) {
            sql.append("uid,");
            count++;
        }
        if (null != s.getMoney()) {
            sql.append("money,");
            count++;
        }
        if (null !=s.getCdate()) {
            sql.append("cdate,");
            count++;
        }
        if (null !=s.getState()) {
            sql.append("state,");
            count++;
        }
        if (null !=s.getDescription()) {
            sql.append("description,");
            count++;
        }        
        sql = new StringBuffer(sql.substring(0, sql.length() - 1));
        sql.append(") value(");
        for (int i = 0; i < (count - 1); i++) {
            sql.append("?,");
        }
        return sql.append("?)").toString();
    }

    private static void parsePS(PreparedStatement ps, Bill s)
        throws SQLException {
        int count = 1;
        if (null != s.getBid()) {
            ps.setLong(count++,s.getBid());
        }
        if (null != s.getCid()) {
            ps.setLong(count++,s.getCid());
        }
        if (null != s.getNum()) {
            ps.setInt(count++,s.getNum());
        }
        if (null != s.getUid()) {
            ps.setLong(count++,s.getUid());
        }
        if (null != s.getMoney()) {
            ps.setInt(count++,s.getMoney());
        }
        if (null != s.getCdate()) {
            ps.setTimestamp(count++,s.getCdate());
        }
        if (null != s.getState()) {
            ps.setShort(count++, s.getState());
        }
        if (null != s.getDescription()) {
            ps.setString(count++, s.getDescription());
        }
    }
}
